/*
 *Copyright (c) 2024 Black Sesame Technologies
 *
 *Licensed under the Apache License, Version 2.0 (the "License");
 *you may not use this file except in compliance with the License.
 *You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 *Unless required by applicable law or agreed to in writing, software
 *distributed under the License is distributed on an "AS IS" BASIS,
 *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *See the License for the specific language governing permissions and
 *limitations under the License.
*/

#include "gmac_time.h"
#include "printf_inc.h"

/* get gmac phc time api */
int gmac_get_phc_time(struct timespec64 *systime)
{
	unsigned int reg_value;

	reg_value = REG32_READ(GMAC_SW_GMAC_INTERNAL + GMAC_MAC_TIMESTAMP_CTRL);
	if (!(reg_value & BIT(0))) {
		hprintf(DEBUG, "ptp clock uninitialized\n");
		return -1;
	}

	/* Get the phc nsec value */
    systime->tv_nsec = REG32_READ(GMAC_SW_GMAC_INTERNAL + GMAC_MAC_SYS_TIME_NANOSEC);
	/* Get the phc sec value */
	systime->tv_sec = REG32_READ(GMAC_SW_GMAC_INTERNAL + GMAC_MAC_SYS_TIME_SEC);
	
	return 0;
}


